package base.study.chapter9;

import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

/**
 * 映射
 */
public class MapDemo1 {

    @Test
    public void testIterator(){
        HashMap<String, Integer> map = new HashMap<>();
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);

        map.forEach((k,v)->{
            System.out.println(k);
            System.out.println(v);
        });
        map.get(1);
    }

    @Test
    public void testDemo2(){
        HashMap<String, Integer> count = new HashMap<>();
        //存放一支值
        count.put("word",1);
        System.out.println(count.size());
        //只有当键原先存在时，才会放入一个值？也就是说，如果 键不存在，则不放值吗？
        Integer word = count.putIfAbsent("word", 0);
        System.out.println(word);
        System.out.println(count.size());
    }

    @Test
    public void testMerge(){
        HashMap<String, Integer> count = new HashMap<>();
        Integer word1 = count.merge("word", 1, new BiFunction<Integer, Integer, Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                System.out.println(integer);
                System.out.println(integer2);
                return integer + integer2;
            }
        });

        System.out.println(word1);

        Integer word = count.get("word");
        System.out.println(word);
    }

    @Test
    public void testViews(){
        List emptyList = Collections.EMPTY_LIST;
        System.out.println(emptyList.size());
    }

    @Test
    public void testViews2(){
        List<String> l = new LinkedList<>();
        l.add("1");
        l.add("2");
        l.add("3");

        List<String> list = l.subList(0, 1);
        list.clear();
        System.out.println(l);
    }

    @Test
    public void testLinkedHashSet(){
        Set<Integer> s = new LinkedHashSet<>();
        s.add(1);
        s.add(2);
        s.add(3);
        s.add(1);
        System.out.println(s);
    }
}